package com.tianyu.inspection.service;

import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tianyu.inspection.common.GeoUtils;
import com.tianyu.inspection.common.component.MapperModel;
import com.tianyu.inspection.common.model.PageModel;
import com.tianyu.inspection.entity.HighConsequenceAreas;
import com.tianyu.inspection.entity.HighConsequenceAreasType;
import com.tianyu.inspection.entity.PileCard;
import com.tianyu.inspection.entity.PipelinePoint;
import com.tianyu.inspection.entity.model.OperateMo;
import com.tianyu.inspection.entity.model.QueryMo;
import com.tianyu.inspection.mapper.HighConsequenceAreasMapper;
import com.tianyu.inspection.mapper.PileCardMapper;
import com.tianyu.inspection.service.api.IHighConsequenceAreasService;
import io.ocs.model.OcsSimpleModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**********************************
 * @Author YSW
 * @Description 高后果区服务
 * @Date 2021/1/18 - 09:33
 **********************************/

@Service
public class HighConsequenceAreasServiceImpl implements IHighConsequenceAreasService {

    @Resource
    private MapperModel mapperModel;

    @Resource
    private HighConsequenceAreasMapper highConsequenceAreasMapper;

    @Resource
    private PileCardMapper pileCardMapper;

    @Resource
    private OcsSimpleModel ocsSimpleModel;

    /**
     * 分页查询
     *
     * @param queryMo 参数模型
     * @return ModelMap
     */
    @Override
    public IPage<HighConsequenceAreas> searchListPage(QueryMo queryMo) {
        Map<Integer, String> pipelineMap = mapperModel.getPipelineMap(queryMo);
        Map<Integer, String> pipelineStationMap = mapperModel.getPipelineStationMap(queryMo);
        Map<Integer, HighConsequenceAreasType> highConsequenceAreasTypeMap = mapperModel.getHighConsequenceAreasTypeMap();
        IPage<HighConsequenceAreas> page = PageModel.getModelPage(queryMo, highConsequenceAreasMapper);
        page.getRecords().forEach(highConsequenceAreas -> {
            if (Objects.nonNull(highConsequenceAreas.getPolygonPoints())){
                highConsequenceAreas.setPolygonPointList(highConsequenceAreas.getPolygonPoints());
                highConsequenceAreas.setPolygonPoints(null);
            }
            final HighConsequenceAreasType highConsequenceAreasType = highConsequenceAreasTypeMap.get(highConsequenceAreas.getHighConsequenceAreasTypeId());
            if (Objects.nonNull(highConsequenceAreasType)){
                highConsequenceAreas.setHighConsequenceAreasTypeName(highConsequenceAreasType.getName());
                final String icon = highConsequenceAreasType.getIcon();
                if (Objects.nonNull(icon)){
                    highConsequenceAreas.setHighConsequenceAreasTypeIcon(ocsSimpleModel.getObjectUrl(icon));
                }
            }
            highConsequenceAreas.setPipelineName(pipelineMap.get(highConsequenceAreas.getPipelineId()));
            highConsequenceAreas.setPipelineStationName(pipelineStationMap.get(highConsequenceAreas.getPipelineStationId()));
        });
        return page;
    }


    /**
     * 通过ID等参数查询一条数据
     *
     * @param queryMo 参数集[实体ID 与 项目ID, 数据状态等]
     * @return 单条记录结果
     */
    @Override
    public HighConsequenceAreas selectOne(QueryMo queryMo) {
        if(Objects.nonNull(queryMo.getEqMap().get("id"))){
            return highConsequenceAreasMapper.selectOne(new QueryWrapper<HighConsequenceAreas>().allEq(queryMo.getEqMap()));
        }
        return null;
    }

    /**
     * 插入一条数据
     *
     * @param operateMo 实体对象
     * @return 是否操作成功
     */
    @Override
    public boolean insert(OperateMo operateMo) {
        ObjectMapper objectMapper = new ObjectMapper();
        HighConsequenceAreas highConsequenceAreas = objectMapper.convertValue(operateMo.getEntity(), HighConsequenceAreas.class);
        return 1 == highConsequenceAreasMapper.insert(highConsequenceAreas);
    }

    /**
     * 通过ID删除一条数据
     *
     * @param queryMo 参数集[实体ID 与 项目ID, 数据状态等]
     * @return 是否操作成功
     */
    @Override
    public boolean delete(QueryMo queryMo) {
        if(Objects.nonNull(queryMo.getEqMap().get("id"))){
            return 1 == highConsequenceAreasMapper.delete(new QueryWrapper<HighConsequenceAreas>().allEq(queryMo.getEqMap()));
        }
        return false;
    }

    /**
     * 插入一条数据
     *
     * @param operateMo 更新参数集
     * @return ModelMap
     */
    @Override
    public boolean update(OperateMo operateMo) {
        ObjectMapper objectMapper = new ObjectMapper();
        HighConsequenceAreas highConsequenceAreas = objectMapper.convertValue(operateMo.getEntity(), HighConsequenceAreas.class);
        return 1 == highConsequenceAreasMapper.update(highConsequenceAreas,new QueryWrapper<HighConsequenceAreas>().allEq(operateMo.getEqMap()));
    }

    /**
     * 通过ID逻辑删除一条数据
     *
     * @param queryMo 参数集[实体ID 与 项目ID, 数据状态]
     * @return 是否操作成功
     */
    @Override
    public boolean logicallyDelete(QueryMo queryMo) {
        return 1 == highConsequenceAreasMapper.update(
                new HighConsequenceAreas().setState(1),
                new QueryWrapper<HighConsequenceAreas>().allEq(queryMo.getEqMap())
        );
    }

    /**
     * 多边形区域构建
     *
     * @param queryMo 起始桩牌信息
     */
    @Override
    public List<PipelinePoint> polygonBuild(QueryMo queryMo) {
        Object projectId = queryMo.get("projectId");
        String distance = (String) queryMo.get("distance");
        Object startPileCardNumber = queryMo.get("startPileCardNumber");
        Object endPileCardNumber = queryMo.get("endPileCardNumber");
        PileCard pileCard1 = pileCardMapper.selectOne(new QueryWrapper<PileCard>()
                .eq("system_stake_number", startPileCardNumber).eq("project_id", projectId));
        if (pileCard1 == null){
            return null;
        }
        PileCard pileCard2 = pileCardMapper.selectOne(new QueryWrapper<PileCard>()
                .eq("system_stake_number", endPileCardNumber).eq("project_id", projectId));
        if (pileCard2 == null){
            return null;
        }
        QueryWrapper<PileCard> wrapper = new QueryWrapper<>();
        wrapper.eq("project_id", projectId);
        if (pileCard1.getId() > pileCard2.getId()){
            wrapper.le("id",pileCard1.getId()).ge("id",pileCard2.getId());
        }else {
            wrapper.le("id",pileCard2.getId()).ge("id",pileCard1.getId());
        }
        List<PileCard> pileCards = pileCardMapper.selectList(wrapper);
        JSONArray jsonArray = new JSONArray();
        for (PileCard point: pileCards){
            jsonArray.add(new PipelinePoint().setLat(point.getLat()).setLng(point.getLng()));
        }
        return GeoUtils.queryBuffer(jsonArray, Integer.parseInt(distance));
    }
}
